package parser

import (
	"regexp"
	"engine"
	"fmt"
	"github.com/garyburd/redigo/redis"
)

var listRe = regexp.MustCompile(`<h3>[^<]+<a href="([^\"]+.shtml)" title="[^<]+" target="_blank">([^<]+)</a>[^<]+</h3>`)
var pageRe = regexp.MustCompile(`<a href='(list[0-9_]+.shtml)'[^>]+>[^<]+</a>`)

func ParseList(contents []byte, baseUrl string) engine.ParseResult {
	matches := listRe.FindAllSubmatch(contents, -1)
	result := engine.ParseResult{}
	c, err := redis.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		fmt.Println("Connect to redis error", err)
	}
	defer c.Close()
	for _, m := range matches {
		title := string(m[2])
		url := string(m[1])
		//判断url是否已经处理了
		ret, err := redis.Int(c.Do("SISMEMBER", "visitedUrls", url))
		if err != nil {
			fmt.Println("redis get failed:", err)
		} else {
			if ret == 1 {
				continue
			}else{
				_, err = c.Do("SADD", "visitedUrls", url)
				if err != nil {
					fmt.Println("redis set failed:", err)
				}

				result.Items = append(result.Items, "List " + title)
				result.Requests = append(result.Requests, engine.Request{
					Url:url,
					ParserFunc:func (c []byte) engine.ParseResult{
						return ParseNews(c,url,title)
					},
				})
			}
		}
	}

	pageMatches := pageRe.FindAllSubmatch(contents, -1)
	for _, m := range pageMatches {
		page := string(m[1])
		url := string(m[1])
		//判断url是否已经处理了
		ret, err := redis.Int(c.Do("SISMEMBER", "visitedUrls", url))
		if err != nil {
			fmt.Println("redis get failed:", err)
		} else {
			if ret == 1 {
				continue
			}else{
				_, err = c.Do("SADD", "visitedUrls", url)
				if err != nil {
					fmt.Println("redis set failed:", err)
				}

				result.Items = append(result.Items, "Page " + page)
				result.Requests = append(result.Requests, engine.Request{
					Url: baseUrl + "/" + url,
					ParserFunc:func (c []byte) engine.ParseResult{
						return ParseList(c, baseUrl)
					},
				})
			}
		}
	}

	return result
}